package com.dataseek.iot.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.device.entity.DayLevel;
import com.dataseek.iot.device.entity.LiquidJar;
import com.dataseek.iot.device.mapper.LiquidJarMapper;
import com.dataseek.iot.device.service.DayLevelService;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/dayLevel")
public class DayLevelController {

    @Autowired
    private DayLevelService dayLevelService;

    @Autowired
    private LiquidJarMapper liquidJarMapper;

    @RequestMapping(value = "/queryDay", method = RequestMethod.GET)
    public ResponseResult queryDay(@RequestParam(value="jarCode", required=false) String jarCode,
                                   @RequestParam(value="deviceCode", required=false) String deviceCode) throws IOException {
        List<DayLevel> reList = dayLevelService.queryDay(jarCode, deviceCode);
        this.addVol(jarCode, reList);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",reList);
    }

    //带fromDate和toDate的液位原始数据查询
    @RequestMapping(value = "/queryDayForchange", method = RequestMethod.GET)
    public ResponseResult queryDayForchange(@RequestParam(value="fromDate", required=false) Date fromDate,
                                   @RequestParam(value="toDate", required=false) Date toDate,
                                   @RequestParam(value="jarCode", required=false) String jarCode,
                                   @RequestParam(value="deviceCode", required=false) String deviceCode) throws IOException {
        List<DayLevel> reList = dayLevelService.queryDayForchange(fromDate, toDate, jarCode, deviceCode);
        this.addVol(jarCode, reList);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",reList);
    }

    @RequestMapping(value = "/queryWeek", method = RequestMethod.GET)
    public ResponseResult queryWeek(@RequestParam(value="jarCode", required=false) String jarCode,
                                   @RequestParam(value="deviceCode", required=false) String deviceCode) throws IOException {
        List<DayLevel> reList = dayLevelService.queryWeek(jarCode, deviceCode);
        this.addVol(jarCode, reList);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",reList);
    }

    //增加厘米到L的换算
    private void addVol(String jarCode, List<DayLevel> tmpList) {
        QueryWrapper<LiquidJar> qw = new QueryWrapper<>();
        qw.eq("jar_code", jarCode);
        LiquidJar jarDto = liquidJarMapper.selectOne(qw);
        if (jarDto != null && jarDto.getJarProportion() != null) {
            //储液罐的换算比例
            BigDecimal jarProp = new BigDecimal(jarDto.getJarProportion());
            for (DayLevel tmpDto : tmpList) {
                tmpDto.setLevelVol(tmpDto.getLevel().multiply(jarProp).setScale(2,BigDecimal.ROUND_HALF_UP));
            }
        }
    }

}
